package frontend.components.base;

import frontend.components.other.UserInputCallableAction;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

public class CheckboxComponent<ModelType> extends Panel {

    public CheckboxComponent(String propertyId, String labelResourceKey, ModelType model, final UserInputCallableAction<Boolean> onClickAction) {
        super(propertyId);
        Label label = new Label("label", new ResourceModel(labelResourceKey));
        final CheckBox checkBox = new CheckBox("checkbox", new PropertyModel<Boolean>(model, propertyId));
        if (onClickAction != null) {
            checkBox.add(new AjaxFormComponentUpdatingBehavior("onchange") {
                @Override
                protected void onUpdate(AjaxRequestTarget target) {
                    Boolean value = checkBox.getModelObject();
                    onClickAction.call(target, value);
                }
            });
        }
        add(label, checkBox);
        setOutputMarkupId(true);
    }
}
